package id.co.shuba.spamobile.widget;

import id.co.shuba.spamobile.R;
import android.content.Context;
import android.database.Cursor;
import android.support.v4.widget.CursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;

public class MaintenanceVisitCursorAdapter extends CursorAdapter {
	private Context mContext;

	public class ViewHolder {
		TextView txtDate;
		TextView txtName;
		TextView txtObjectiveOfVisit;
		ImageButton btnDelete;
	}

	public MaintenanceVisitCursorAdapter(Context context, Cursor c, int flags) {
		super(context, c, flags);
		mContext = context;
	}

	public MaintenanceVisitCursorAdapter(Context context, Cursor c, boolean autoRequery) {
		super(context, c, autoRequery);
		mContext = context;
	}

	@Override
	public View newView(Context context, Cursor cursor, ViewGroup parent) {
		View view = LayoutInflater.from(mContext)
				.inflate(R.layout.maintenance_visit_item , parent, false);
		ViewHolder viewHolder = new ViewHolder();
		viewHolder.txtDate = (TextView) view.findViewById(R.id.txtDate);
		viewHolder.txtName = (TextView) view.findViewById(R.id.txtName);
		viewHolder.txtObjectiveOfVisit = (TextView) view.findViewById(R.id.txtTest);
		viewHolder.btnDelete = (ImageButton) view.findViewById(R.id.btnDelete);
		view.setTag(viewHolder);
		return view;
	}

	@Override
	public void bindView(View view, Context context, Cursor cursor) {
		ViewHolder viewHolder = (ViewHolder) view.getTag();
		String[] arr = cursor.getString(cursor.getColumnIndex("DATE_VISIT")).split(" ");
		viewHolder.txtDate.setText(arr[0]);
		viewHolder.txtName.setText(cursor.getString(cursor.getColumnIndex("DEBTOR_NAME")));
		viewHolder.txtObjectiveOfVisit.setText(cursor.getString(cursor.getColumnIndex("OBJECTIVE_OF_VISIT")));
		viewHolder.btnDelete.setFocusable(false);
		viewHolder.btnDelete.setTag(cursor.getString(cursor.getColumnIndex("VISIT_ID")));
	}
}
